package org.example.user.controller;

import com.mybatisflex.core.paginate.Page;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import org.example.common.config.Result;
import org.example.common.enums.LogAsync;
import org.example.user.dto.RealnameAuthDTO;
import org.example.user.dto.CreateUserDTO;
import org.example.user.dto.UpdateDTO;
import org.example.user.dto.UserQueryDTO;
import org.example.user.service.ISysUserRealnameAuthService;
import org.example.user.service.MockSmsCodeService;
import org.example.user.vo.SysUserVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.example.user.service.ISysUserService;

/**
 * 用户表 控制层。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@RestController
@RequestMapping("/api/user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private MockSmsCodeService smsCodeService;

    @Autowired
    private ISysUserRealnameAuthService sysUserRealnameAuthService;

    @LogAsync("创建用户")
    @PostMapping("/create")
    public Result create(@RequestBody @Valid CreateUserDTO createUserDTO) {
        int result = sysUserService.createUser(createUserDTO);
        return Result.ok(result);
    }

    //修改用户
    @LogAsync("修改用户")
    @PutMapping("/update")
    public Result update(@RequestBody @Valid UpdateDTO updateDTO) {
        int result = sysUserService.updateUser(updateDTO);
        return Result.ok(result);
    }

    //删除用户
    @LogAsync("删除用户")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long userId){
        int result = sysUserService.deleteUser(userId);
        return Result.ok(result);
    }

    //获取用户列表
    @GetMapping("/list")
    public Result list(@ModelAttribute UserQueryDTO userQueryDTO){
        Page<SysUserVO> list = sysUserService.pageList(userQueryDTO);
        return Result.ok(list);
    }

    //获取用户详情
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable("id") Long userId){
        return Result.ok(sysUserService.getUserInfo(userId));
    }

    //重置密码
    @PutMapping("/resetPwd")
    public Result resetPwd(@RequestParam("userId") Long userId,
                           @RequestParam("password") String password){
        return Result.ok(sysUserService.resetPwd(userId, password));
    }

    //获取个人信息
    @GetMapping("/profile")
    public Result profile(){
        return Result.ok(sysUserService.getUserProfile());
    }

    //修改个人信息
    @PutMapping("/updateProfile")
    public Result updateProfile(
            @RequestParam("name") String name,
            @RequestParam("email")  @Email(message = "邮箱格式不正确") String email
    ){
        return Result.ok(sysUserService.updateUserProfile(name, email));
    }

    //修改密码
    @PutMapping("/password")
    public Result updatePassword(
            @RequestParam("oldPassword")  @NotBlank(message = "原密码不能为空")  String oldPassword,
            @RequestParam("newPassword")  @Pattern(regexp = "^(?=.*[a-zA-Z])(?=.*\\d).+$", message = "密码必须包含字母和数字") String newPassword
    ){
        return Result.ok(sysUserService.updatePassword(oldPassword, newPassword));
    }

    //获取验证码
    @PutMapping("/phone/code")
    public Result getSmsCode(
            @RequestParam @Pattern(regexp = "^1[3-9]\\d{9}$") String phone) {

        String code = smsCodeService.sendCode(phone);
        return Result.ok(code);
    }

    //更换绑定手机号
    @PutMapping("/phone")
    public Result updatePhone(
            @RequestParam @Pattern(regexp = "^1[3-9]\\d{9}$") String phone,
            @RequestParam String code
    ){
        return Result.ok(sysUserService.updatePhone(phone, code));
    }

    //实名认证
    @PostMapping("/auth-submit")
    public Result authSubmit(@RequestBody @Valid RealnameAuthDTO authDTO){
        return Result.ok(sysUserRealnameAuthService.authSubmit(authDTO));
    }

    @GetMapping("/auth-status")
    public Result authStatus(){
        return Result.ok(sysUserRealnameAuthService.getUserAuthStatus());
    }

}